package com.health.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.health.dto.DepartmentPageQueryDTO;
import com.health.entity.Department;
import com.health.mapper.DepartmentMapper;
import com.health.result.PageResult;
import com.health.service.DepartmentService;
import com.health.vo.DepartmentDateVO;
import com.health.vo.DepartmentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class DepartmentServiceImp implements DepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    // 科室列表
    @Override
    public List<DepartmentVO> pageQuery(DepartmentPageQueryDTO departmentPageQueryDTO) {
        PageHelper.startPage(departmentPageQueryDTO.getPage(), departmentPageQueryDTO.getPageSize());

        Page<Department> page = departmentMapper.pageQuery(departmentPageQueryDTO);
        //1.得到一个科室的集合
        List<Department> departmentList = page.getResult();
        //1.1判断科室集合是否有子科室child：parent是null则为一级科室，放外层，有parent的说明为二级科室放内层
        //过滤Parent() != null
        List<Department> list = departmentList.stream().filter(department -> department.getParent() != null).collect(Collectors.toList());
        //分组 key:上级科室parentId 和  value:科室集合
        Map<Long, List<Department>> map = list.stream().collect(Collectors.groupingBy(Department::getParent));
        List<DepartmentVO> vos = departmentList.stream().map(item -> {
            DepartmentVO vo = new DepartmentVO();
            vo.setId(item.getId());
            vo.setName(item.getName());
            vo.setChild(map.get(item.getId()));
            return vo;
        }).collect(Collectors.toList());
        return vos;
    }

    @Override
    public List<DepartmentDateVO> getDate() {
        //获取当前的日期
        LocalDate begin = LocalDate.now();
        List<DepartmentDateVO> dateVOList = new ArrayList<>();
//        LocalDate begin = now.minusDays(1);
        LocalDate end = begin.plusDays(7);
        //获取日期列表 localDateList 如1.20-1.26
        while (!begin.equals(end)) {
            DepartmentDateVO dateVO = new DepartmentDateVO();
            //获取当前日期的天
            Long day = (long) begin.getDayOfMonth();
            //得到这一周的星期几
            DayOfWeek weekday1 = begin.getDayOfWeek();
            String weekday = weekday1.toString();
            //weekday.getValue()得到本月1号int类型的星期几
//            int value = weekday.getValue();
            if (weekday.equals("SUNDAY")) {
                weekday = "周日";
            } else if (weekday.equals("MONDAY")) {
                weekday = "周一";
            } else if (weekday.equals("TUESDAY")) {
                weekday = "周二";
            } else if (weekday.equals("WEDNESDAY")) {
                weekday = "周三";
            } else if (weekday.equals("THURSDAY")) {
                weekday = "周四";
            } else if (weekday.equals("FRIDAY")) {
                weekday = "周五";
            } else if (weekday.equals("SATURDAY")) {
                weekday = "周六";
            }
            dateVO.setDate(begin);
            //判断是否是当前日期，如果是就设置成当日
            LocalDate now1 = LocalDate.now();
            if (begin.equals(now1)){
                dateVO.setWeek("当日");
            }else {
                dateVO.setWeek(weekday);
            }
            dateVO.setNumber(day);
            dateVOList.add(dateVO);
            begin = begin.plusDays(1);
        }

        return dateVOList;
    }
}
